package com.nondo.rdp.auth.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.Predicate;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.rdp.auth.domain.Message;
import com.nondo.rdp.auth.domain.User;
import com.nondo.rdp.auth.domain.UserMessage;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.auth.repository.UserMessageRepository;
import com.nondo.rdp.auth.repository.UserRepository;
import com.nondo.rdp.auth.service.MessageService;
import com.nondo.rdp.auth.service.UserMessageService;
import com.nondo.rdp.core.common.StatusConstant;

@Transactional
@Service
public class UserMessageServiceImpl implements UserMessageService {
	@Autowired
	UserMessageRepository userMessageRepository;

	@Autowired
	UserRepository userRepository;

	@Autowired
	MessageService messageService;

	@Override
	public UserMessage findOne(Long id) {
		return this.userMessageRepository.findOne(id);
	}

	@Override
	public UserMessage save(UserMessage userMessage) {
		return this.userMessageRepository.save(userMessage);
	}

	@Override
	public void delete(Long id) {
		this.userMessageRepository.deleteById(id);
	}

	@Override
	public Page<UserMessage> findAll(Page<UserMessage> page, UserMessage userMessage) {
		Sort sort = new Sort(Sort.Direction.DESC, "createDate");
		PageRequest pageRequest = new PageRequest(page.getPageNumber(),
				page.getPageSize(), sort);
		org.springframework.data.domain.Page<UserMessage> dictPage = userMessageRepository.findAll((root, query, builder) -> {
			List<Predicate> predicates = new ArrayList<>();
			if (userMessage != null) {
				if(userMessage.getType() != null){
					predicates.add(builder.equal(root.get("message").get("type"), userMessage.getType()));
				}
				if (userMessage.getUser() != null) {
					predicates.add(builder.equal(root.get("user").get("id"), userMessage.getUser().getId()));
				}
			}
			predicates.add(builder.like(root.get("delFlag"),StatusConstant.DEL_FLAG_NORMAL));
			predicates.add(builder.like(root.get("message").get("delFlag"),StatusConstant.DEL_FLAG_NORMAL));
			Predicate[] predicatesArray = new Predicate[predicates.size()];
			predicates.toArray(predicatesArray);
	
			return builder.and(predicatesArray);
	
		}, pageRequest);

		return new Page<UserMessage>(dictPage);
	}

	@Override
	public void update(Long mid, Long uid) {
		this.userMessageRepository.update(mid, uid);
	}

	@Override
	public void saveUserMessage(Message message) {
		if (message.getPublishDate() == null) {
			message.setPublishDate(new Date());
		}
		String uids = message.getUids();
		if (StringUtils.isBlank(uids)) {
			List<User> userList = userRepository.getAll();
			for (User user : userList) {
				UserMessage userMessage = saveUserMessage(message, user);
				message.setUmid(userMessage.getId());
			}
		} else {
			String[] uidArray = uids.split(",");
			for (String uid : uidArray) {
				User user = userRepository.findOne(Long.parseLong(uid));
				UserMessage userMessage = saveUserMessage(message, user);
				message.setUmid(userMessage.getId());
				
			}
		}
	}

	private UserMessage saveUserMessage(Message message, User user) {
		this.messageService.save(message);
		UserMessage userMessage = new UserMessage();
		userMessage.setUser(user);
		userMessage.setMessage(message);
		userMessage = this.userMessageRepository.save(userMessage);
		return userMessage;
	}

	@Override
	public int getUnReadNumByUserId(Long uid) {
		return this.userMessageRepository.count(uid);
	}

}